const fs = require("fs-extra");
const path = require("path");

async function readDir(path) {
    let dirs = await fs.readdir(path);
    if (!dirs) {
        throw "There is no direcory in the 'public/img' file";
    }
    return dirs;
}

async function readFile(path) {
    let dirs = await readDir(path);
    // console.log(dirs);
    let res = []; // 对象数组
    for (let dir of dirs) {
        let files = await fs.readdir(path + "/" +　dir);
        
        if (dir === "默认相册") {
            res.unshift({
                albumName: dir,
                filenames: files
            });
        } else {
            res.push({
                albumName: dir,
                filenames: files 
            });
        }
    }
    // console.log(res);
    return res;
}

module.exports = readFile;